<template>
	<view class="login pd-20">
		<view class="user mb-20">
			<view class="lognav">
				<!-- {{u.cdid}} -- {{u.yid}} -->
				<view class="label">车牌：</view>
				<input type="text" class="inputnav" placeholder="请输入车牌" v-model="u.cp" />
			</view>
			<view class="lognav">
				<view class="label">手机号：</view>
				<input type="text" class="inputnav" placeholder="请输入手机号" v-model="u.phone" />
			</view>
			<view class="lognav">
				<view class="label">司机名称：</view>
				<input type="text"  class="inputnav" placeholder="请输入司机名称" v-model="u.name" />
			</view>
			<view class="lognav">
				<view class="label" >身份证号：</view>
				<input type="text"  class="inputnav" placeholder="请输入身份证号" v-model="u.sfz" />
			</view>
		<!-- 	<view class="lognav">
				<view class="label" >货物型号：</view>
				<input type="text"  class="inputnav" placeholder="请输入货物型号" v-model="u.hinfo" />
			</view> -->
			<view class="lognav">
				<view class="label" >备注：</view>
				<input type="text"  class="inputnav" placeholder="请输入备注" v-model="u.content" />
			</view>
		</view>
		<view class="loginbut">
			<button type="primary" class="mb-20"  open-type="getUserInfo" @click="add">绑定</button>
		</view>
			
		
	</view>
</template>

<script>
	export default {
		data() {
			return {
				uid:'',
				code:'',
				// appid: "wx92f28e9b99acb503",
				appid:'wx544a6c03c80c9eac',
				uinfo:'',
				yinfo:'',
				cdinfo:'',
				u:{	
					uid:'',
					yid:'',
					cdid:'',
					sfz:'',
					content:'',
					// hinfo:'',
					cp:'',
					phone:'',
					name:''
				}
			}
		},
		async onLoad(e){
			this.u.cdid = e.cdid
			this.u.yid = e.yid
			let uid = uni.getStorageSync('uid')
				if(uid==null || uid==''){
					uni.switchTab({
						url:'/pages/my/my'
					})
				}
			let data = {
				id:uni.getStorageSync('uid')
			}
			let udata =  await this.$api.getuserinfo(data)
			if(udata.data.data!=null){
					this.getuserinfo();
			}else{
					this.wechatLogin();
			}
		},
		methods: {
			//获取我当前预约的绑定信息判断是否绑定
			async getjilubdtype(){
				let data = {
					 cdid:this.u.cdid,
					 yid:this.u.yid,
					 uid:uni.getStorageSync('uid')
				}
				let res =  await this.$api.getjilubdtype(data)
				console.log(res.data.data)
				if(res.data.data!=null){
					uni.navigateTo({
						url:'/pages/detailyuy/detailyuy?id='+this.u.yid
					})
				}
				
			},
			
			GetUrlParam(paraName) {
				//微信回调 回回调当前请求的域名 所以需要从链接后获取参数
					var url = document.location.toString();
					var arrObj = url.split("?");
					if (arrObj.length > 1) {
						var arrPara = arrObj[1].split("&");
						var arr;
						for (var i = 0; i < arrPara.length; i++) {
							arr = arrPara[i].split("=");
							if (arr != null && arr[0] == paraName) {
								return arr[1];
							}
						}
						return "";
					} else {
						return "";
					}
				},
			async getuserinfo(){
				let data = {
					id:uni.getStorageSync('uid')
				}
				let res =  await this.$api.getuserinfo(data)
				console.log(res.data.data)
				if(res.data.code==0){
					this.uinfo = res.data.data
					this.u.uid = res.data.data.id
					this.u.cp = res.data.data.cp
					this.u.name = res.data.data.cname
					this.u.phone = res.data.data.phone
					this.u.sfz = res.data.data.sfz
					this.getjilubdtype()
				}
			},
			async add(){
					if(this.u.cp==''){
							uni.showToast({
							  title: '请填写车牌',	
							  icon:'none',
							  duration: 2000
							});
							return false;
					}
				if(this.u.name==''){
						uni.showToast({
						  title: '请填写司机名称',	
						  icon:'none',
						  duration: 2000
						});
						return false;
				}
				if(this.u.phone==''){
						uni.showToast({
						  title: '请填写手机号',	
						  icon:'none',
						  duration: 2000
						});
						return false;
				}
				if(this.u.sfz==''){
						uni.showToast({
						  title: '请填写身份证',
						  icon:'none',
						  duration: 2000
						});
						return false;
				}
				
				
					let res =  await this.$api.bangding(this.u)
					if(res.data.code==0){
				
						uni.navigateTo({
							url:'/pages/detailyuy/detailyuy?id='+this.u.yid
						});
					}else{
						uni.showToast({
						  title: res.data.msg,	
						  icon:'none',
						  duration: 2000
						});
					}
			},
			//登录接口
			async wechatLogin(){
						this.code = this.GetUrlParam('code'); 
						if (!this.code) {
							let uri = encodeURIComponent(window.location.href);
							window.location.href = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + 
							this.appid +
							"&redirect_uri=" +uri + "&response_type=code&scope=snsapi_userinfo&state=state&wechat_redirect&connect_redirect=1#wechat_redirect";
					
						}else{
							//设置登录信息
							let res = 	 await this.$api.acctokencode({code:this.code})
							if(res.data.code==0){
								//进行缓存数据	
									uni.setStorageSync('uid', res.data.data.id);
									console.log(res.data.data)
									this.uinfo = res.data.data
									this.u.cp = res.data.data.cp
									this.u.name = res.data.data.cname
									this.u.phone = res.data.data.phone
									this.u.sfz = res.data.data.sfz
									this.getjilubdtype()
									
							}else{
								uni.showToast({
								  title: '登录异常 重新扫码登录',	
								  icon:'none',
								  duration: 2000
								});
							}	
						}
					},
						
		}
		
	}
</script>

<style lang="scss">
	.lognav{
		width: 100%;
		height: 70px;
		padding: 10rpx;
		margin-bottom: 10px;
	}
	.inputnav{
			border: 1rpx solid #555555;
			height: 30px;
			padding: 10rpx;
			margin: 10px;
			border-radius: 5px;
		}
		.label{
			width: 30%;	
		}
		.loginbut{
			width: 60%;
			margin: 0 auto;
		}
</style>
